﻿function Install-WorkflowManager
{
    <# 
     .Synopsis
      Launches the Web Platform Installer in offline mode to install Workflow Manager 1.0 Refresh

     .Description
      This script launches the Web Platform Installer in offline mode to install Workflow Manager 1.0 Refresh

     .Parameter WebPiProductListXmlFile
       The full file system path where the offline cache of installation source files for Workflow Manager are located. 
       The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\13-Workflow\feeds\latest\webproductlist.xml'

     .Parameter WebPiExePath
       The full file system path where the Web Platform Installer executable file (WebpiCmd.exe) is located. 
	   The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\13-Workflow\bin\WebPiCmd.exe'

     .Example
       Install-WorkflowManager 

     .Example
       Install-WorkflowManager -WebPiProductListXmlFile 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\13-Workflow\feeds\latest\webproductlist.xml'

    #>
    [CmdletBinding(DefaultParametersetName="CustomPaths")]
    param([parameter(ParameterSetName = "CustomPaths", 
                     Mandatory = $false,
                     HelpMessage = "The full file system path where the offline cache of installation source files for Workflow Manager are located. The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\13-Workflow\feeds\latest\webproductlist.xml'")]
                     [string]$WebPiProductListXmlFile = ($env:ProgramData + "\SharePoint Total Automation Toolkit\Software\13-Workflow\feeds\latest\webproductlist.xml"),
          [parameter(ParameterSetName = "CustomPaths", 
                     Mandatory = $false,
                     HelpMessage = "The full file system path where the Web Platform Installer executable file (WebpiCmd.exe) is located. The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\13-Workflow\bin'")]
                     [string]$WebPiExePath = ($env:ProgramData + "\SharePoint Total Automation Toolkit\Software\13-Workflow\bin\WebPiCmd.exe"))

    switch ($PSCmdlet.ParameterSetName)
    {
        "CustomPaths"
        {
            $Restart = $false
        }    
        default
        {
            $Restart = $false
        }    
    }

	$rebootStatus = Get-PendingReboot
	if ($rebootStatus.RebootPending -eq $true)
	{
		Write-Host "NOTE: A system restart is required. Please restart the system prior to continuing with the installation." -ForegroundColor Yellow
		Write-Host "      When the system restarts please rerun this script until you no longer receive a restart request." -ForegroundColor Yellow
		throw 
	}

    # Initialize Progress Variables
    [int]$progressBarId = 100
    $startTime = [System.DateTime]::Now

    Write-Host
    Write-Host "Beginning the Workflow Manager installation."

    #region Task 1

    # Launch the Web Platform Installer and Install Workflow Manager 1.0 Refresh, Windows Azure Pack: Service Bus 1.1
    $setupExe = ($WebPiExePath)
    $setupArgs = ('/Install /AcceptEULA /SuppressPostFinish /SuppressReboot /Products:"WorkflowManagerRefresh" /XML:"{0}"' -f $WebPiProductListXmlFile)
    $setupExeInstance = [System.Diagnostics.Process]::Start($setupExe, $setupArgs)
    $step = 5
    $value = 0

    while (-not ($setupExeInstance.HasExited)){
        $currentRuntime = [System.DateTime]::Now - $startTime
        Write-Progress -Id $progressBarId -Activity "Installing Workflow Manager" -PercentComplete ($value+=$step) -CurrentOperation "Executing the Web Platform Installer" -Status ("The current operation has been running for {0}:{1}:{2}" -f $currentRuntime.Hours.ToString("00"), $currentRuntime.Minutes.ToString("00"), $currentRuntime.Seconds.ToString("00"))
     
        if ($value -ge 100) { $value = 0}

        Start-Sleep -Seconds 1
    }

    if ($setupExeInstance.ExitCode -eq 0)
    {
        Write-Host "      Workflow Manager Successfully Installed"
    }
    elseif ($setupExeInstance.ExitCode -eq 3010)
    {
        if ($Restart -eq $true)
        {
            Restart-Computer
        }
        else
        {
            Write-Host "NOTE: A system restart is required. Please restart the system prior to continuing with the Workflow Manager installation." -ForegroundColor Yellow
            Write-Host "      When the system restarts please rerun this script until you no longer receive a restart request." -ForegroundColor Yellow
        }
    }
    else
    {
        Write-Error "     Installation of Workflow Manager Failed"
        throw
    }

    #endregion

    Write-Progress -Id $progressBarId -Activity "Installing Workflow Manager" -Status "Finishing" -PercentComplete 100 -CurrentOperation "" -Completed

    Write-Host " Workflow Manager installation complete."
    Write-Host
   
}